import { Module, Scope } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Flavor } from './entities/flavor.entity';
import { Coffee } from './entities/coffee.entity';
import { CoffeeController } from './coffee.controller';
import { CoffeeService } from './coffee.service';
import { COFFEE_BRAND } from './coffee.constant';
import { Connection } from 'typeorm';
import { ConfigModule } from '@nestjs/config';
import coffeeConfig from './config/coffee.config';

class configService {}
class DevelopmentConfigService {}
class ProductionConfigService {}

@Module({
  imports: [
    TypeOrmModule.forFeature([Coffee, Flavor]),
    // ConfigModule,
    ConfigModule.forFeature(coffeeConfig),
  ],
  controllers: [CoffeeController],
  providers: [
    CoffeeService,
    /* {
      provide: COFFEE_BRAND,
      useValue: ['buddy brew', 'nescafe'],
    }, */
    /* 
      如果当我们的项目启动，需要请求多个异步的接口才进行下一步时，那我们可以进行如下配置
      例如，例如在我们数据库建立连接之前，我们不想有任何的请求
      例如，COFFEE_BRAND 提供的数据，是从数据库中获取的
    */
    {
      provide: COFFEE_BRAND,
      useFactory: async (connection: Connection) => {
        /* 
          这里用Promise来模拟一个数据库的请求
          实际是：
          const coffeeBrand = connection.query('...')
        */
        const coffeeBrand = await Promise.resolve(['buddy brew', 'nescafe']);

        return coffeeBrand;
      },
      inject: [Connection],
      // scope: Scope.REQUEST
    },
    {
      provide: configService,
      useClass:
        process.env.NODE_ENV === 'development'
          ? DevelopmentConfigService
          : ProductionConfigService,
    },
  ],
  exports: [CoffeeService],
})
export class CoffeeModule {}
